//
//  WebInfo_GetDetail_Task.m
//  EpointFrame5
//
//  Created by 李亚春 on 14-1-21.
//  Copyright (c) 2014年 Epoint. All rights reserved.
//

#import "WebInfo_GetDetail_Task.h"
#import "EpointWebInfoSingleRecordModel.h"
#import "EpointWebInfoDetailAttachModel.h"
#import "EpointDbConfig.h"

@implementation WebInfo_GetDetail_Task

-(void)execute
{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSString *ValidateData = [EpointBizTools getToken];
    NSString *wsNamespace = NSLocalizedString(@"Epoint_DNet_WS_NameSpace", @"");
    NSString *url = self.getMobileOAWebserviceURL;
    NSString *userguid = [userDefaults objectForKey:Epoint_User_UserGuid];
    NSString *InfoGuid = [params objectForKey:@"InfoGuid"];
    
    NSString *ParasXml = @"<![CDATA[<?xml version=\"1.0\" encoding=\"gb2312\"?><paras><UserGuid>%@</UserGuid><InfoGuid>%@</InfoGuid></paras>]]>";
    
    ParasXml = [NSString stringWithFormat:ParasXml,userguid,InfoGuid];
    
    EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"WebInfo_GetDetail"];
    [ws addParameterForString:@"ValidateData" value:ValidateData];
    [ws addParameterForString:@"ParasXml" value:ParasXml];
    NSString *bs =  ws.start;
    if ([self validateXMLWS:bs]) {
        
        [self exeSuccessWithData:[self getAnasModel:bs]];
    }
//    NSMutableArray *result = [self validateXMLWS:bs];
//    NSString *msg = [result objectAtIndex:1];
//    if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
//        [DBFrameService setCacheValue:ParasXml value:bs];
//        [self exeSuccessWithData:[self getAnasModel:bs]];
//    }
//    else
//    {
//        [self userCache:ParasXml message:msg];
//    }
    
//    //开始请求,判断网络是否可用
//    if (![self isNetWorkAvailable])
//    {
//        [self userCache:ParasXml message:@"网络请求失败，请检查网络配置！"];
//    }
//    else
//    {
//        //请求接口
//        EpointWebServiceUtil *ws = [[EpointWebServiceUtil alloc]initWithUrl:url namespace:wsNamespace method:@"WebInfo_GetDetail"];
//        [ws addParameterForString:@"ValidateData" value:ValidateData];
//        [ws addParameterForString:@"ParasXml" value:ParasXml];
//        NSString *bs =  ws.start;
//        
//        NSMutableArray *result = [self validateXMLWS2:bs];
//        NSString *msg = [result objectAtIndex:1];
//        if ([[result objectAtIndex:0]isEqualToString:@"1"]) {
//            [DBFrameService setCacheValue:ParasXml value:bs];
//            [self exeSuccessWithData:[self getAnasModel:bs]];
//        }
//        else
//        {
//            [self userCache:ParasXml message:msg];
//        }
//    }
}

-(void)userCache:(NSString *)key message:(NSString *)msg
{
    //获取缓存数据
    NSString *cacheBS = [DBFrameService getCacheValue:key];
    
    //判断是否存在缓存
    if (cacheBS.length>0) {
        [self exeSuccessWithData:[self getAnasModel:cacheBS]];
    }
    else
    {
        [self exeFailureWithMessage:msg];
    }
}

-(NSMutableArray *)getAnasModel:(NSString *)bs
{
    NSString *xmldata = [EpointStringUtil getXMLFormatedAttributeBsWithTag:bs xmlTag:@"UserArea"];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithXMLString:xmldata  options:0 error:nil];
    GDataXMLElement *rootElement = [doc rootElement];
    NSArray *arrays = [rootElement elementsForName:@"SigleInfoRecord"];
    NSMutableArray *listmp = [[NSMutableArray alloc]init];
    for (GDataXMLElement *arr in arrays)
    {
        EpointWebInfoSingleRecordModel *m = [[EpointWebInfoSingleRecordModel alloc]init];
        GDataXMLElement *title = [[arr elementsForName:@"title"] objectAtIndex:0];
        m.title = [title stringValue];
        
        GDataXMLElement *infoProperty = [[arr elementsForName:@"infoProperty"] objectAtIndex:0];
        m.infoProperty = [infoProperty stringValue];
        
        GDataXMLElement *content = [[arr elementsForName:@"content"] objectAtIndex:0];
        m.content = [content stringValue];
        
        GDataXMLElement *LinkUrl = [[arr elementsForName:@"LinkUrl"] objectAtIndex:0];
        m.LinkUrl = [LinkUrl stringValue];
        
        GDataXMLElement *infoDate = [[arr elementsForName:@"infoDate"] objectAtIndex:0];
        m.infoDate = [infoDate stringValue];
        
        GDataXMLElement *postUserName = [[arr elementsForName:@"postUserName"] objectAtIndex:0];
        m.postUserName = [postUserName stringValue];
        
        GDataXMLElement *AttachFiles = [[arr elementsForName:@"AttachFiles"] objectAtIndex:0];
        NSArray *SingleFileArray = [AttachFiles elementsForName:@"SingleFile"];
        
        m.AttachFiles = [[NSMutableArray alloc]init];
        
        for (GDataXMLElement *SingleFile in SingleFileArray)
        {
            EpointWebInfoDetailAttachModel *am = [[EpointWebInfoDetailAttachModel alloc]init];
            
            GDataXMLElement *AttachGuid = [[SingleFile elementsForName:@"AttachGuid"] objectAtIndex:0];
            am.AttachGuid = [AttachGuid stringValue];
            
            GDataXMLElement *AttFileName = [[SingleFile elementsForName:@"AttFileName"] objectAtIndex:0];
            am.AttFileName = [AttFileName stringValue];
            
            GDataXMLElement *isBigFile = [[SingleFile elementsForName:@"isBigFile"] objectAtIndex:0];
            am.isBigFile = [isBigFile stringValue];
            
            GDataXMLElement *DownLoadUrl = [[SingleFile elementsForName:@"DownLoadUrl"] objectAtIndex:0];
            am.DownLoadUrl = [DownLoadUrl stringValue];
            
            [m.AttachFiles addObject:am];
        }
        [listmp addObject:m];
    }
    
    return listmp;
}

@end
